package leetcode.editor.cn;

import java.util.HashMap;

public class TwoSum1{
    public static void main(String[] args) {
        Solution solution = new TwoSum1().new Solution();

//           solution.twoSum()
    }
    //leetcode submit region begin(Prohibit modification and deletion)
    class Solution {
        public int[] twoSum(int[] nums, int target) {
            if (nums==null || nums.length<2){
                return null;
            }
            //find first, get second
            HashMap<Integer, Integer> hashMap = new HashMap<>();
            for (int i = 0; i < nums.length; i++) {
                int last = target - nums[i];
                //find last
                if (hashMap.containsKey(nums[i])){
                    return new int[]{hashMap.get(nums[i]),i};
                }
                //cache last,and first key.
                hashMap.put(last,i);
            }
            return null;
        }
    }
//leetcode submit region end(Prohibit modification and deletion)
}